using Godot;
using System;

public partial class ArenaTimeManager : Node
{
	[Export]
	public PackedScene endScreenScene;
	[Signal] 
	public delegate void ArenaDifficultyIncreasedEventHandler(int arenaDifficulty);
	private Timer timer;
	private MetaProgression metaProgression;
	public int arenaDifficulty = 0;
	private const int DIFFICULTY_INTERVAL = 5;
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		metaProgression = GetNode<MetaProgression>("/root/MetaProgression");
		timer = GetNode<Timer>("Timer");

		timer.Timeout += OnTimerTimeout;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		double nextTimeTarget = timer.WaitTime - (arenaDifficulty + 1) * DIFFICULTY_INTERVAL;
		if(nextTimeTarget > timer.TimeLeft){
			arenaDifficulty++;
			this.EmitSignal(nameof(ArenaDifficultyIncreased),arenaDifficulty);
		}
	}

	public double GetTimeElapsed(){
		return timer.WaitTime - timer.TimeLeft;
	}

	public void OnTimerTimeout(){
		EndScreen endScreen = endScreenScene.Instantiate() as EndScreen;
		AddChild(endScreen);
		endScreen.PlayJingle();
		metaProgression.Save();
		
	}
}
